<template>
  <div class="container">
    <h1 class="page-title">就诊记录</h1>

    <div class="card">
      <h3 class="section-title">医嘱详情</h3>
      <div class="card-body">
        <div class="advice-section">
          <div class="advice-row">
            <div class="advice-label">诊断结果</div>
            <div class="advice-content">{{ emergencyRecord.medical_records?.diagnosis }}</div>
          </div>

          <div class="advice-row">
            <div class="advice-label">主要症状</div>
            <div class="advice-content">{{ emergencyRecord.medical_records.main_symptoms }}</div>
          </div>

          <div class="advice-row">
            <div class="advice-label">治疗方案</div>
            <div class="advice-content">
              <div v-for="(item, index) in emergencyRecord.medical_records.treatment_plan"
                   :key="index"
                   class="advice-item">
<!--                <div class="advice-item-number">{{ index + 1 }}.</div>-->
                <div class="advice-item-content">{{ item }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="card">
      <h3 class="section-title">处方信息</h3>
      <div class="card-body">
        <table class="table">
          <thead>
          <tr>
            <th>药品名称</th>
            <th>剂量</th>
            <th>用法用量</th>
            <th>用药周期</th>
            <th>状态</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(prescription, index) in emergencyRecord.medical_records.prescriptions"
              :key="index">
            <td>{{ prescription.drug_name }}</td>
            <td>{{ prescription.specification }}</td>
            <td>{{ prescription.usage }}</td>
            <td>{{ prescription.duration }}</td>
            <td>
                <span class="status-tag" :class="statusClass(prescription.status)">
                  {{ prescription.status }}
                </span>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>

    <div class="card">
      <h3 class="section-title">痛风发作记录</h3>
      <div class="card-body">
        <div class="chart">
          <div ref="laboratoryRef" style="width: 100%; height: 100%;"></div>
        </div>
        <div class="indicators">
          <div class="indicator-card indicator-card-blue">
            <div class="indicator-value">{{ emergencyRecord.medical_records.uric_acid_levels.current }}</div>
            <div class="indicator-title">血尿酸浓度</div>
            <div class="indicator-change text-success">
              {{ emergencyRecord.medical_records.uric_acid_levels.trend }}
            </div>
          </div>

          <div class="indicator-card indicator-card-green">
            <div class="indicator-value">{{ emergencyRecord.medical_records.uric_acid_levels.is_miss_drug }}</div>
            <div class="indicator-title">近期是否有漏服</div>
          </div>

          <div class="indicator-card indicator-card-orange">
            <div class="indicator-value">{{ emergencyRecord.medical_records.uric_acid_levels.is_change_drug }}</div>
            <div class="indicator-title">近期是否更换药品</div>
          </div>
        </div>
      </div>
    </div>

    <div class="card">
      <h3 class="section-title">近期复发情况</h3>
      <div class="card-body">
        <div class="indicators">
          <div class="indicator-card indicator-card-purple">
            <div class="indicator-value">{{ emergencyRecord.medical_records.uric_acid_levels.is_recurrency }}</div>
            <div class="indicator-title">近期是否有复发</div>
            <div class="indicator-change">{{ emergencyRecord.medical_records.uric_acid_levels.time_frame }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getEmergencyRecords } from '@/demo-data/data/records.js'
import { getPatientById } from '@/demo-data/data/patients.js'
import * as echarts from 'echarts'

const route = useRoute()
const patient = ref(null)
// 修改为对象结构初始化
const emergencyRecord = ref({
  medical_records: {
    diagnosis: '',
    main_symptoms: '',
    treatment_plan: [],
    prescriptions: [],
    uric_acid_levels: {
      current: '',
      trend: '',
      is_miss_drug: '',
      is_change_drug: '',
      is_recurrency: '',
      time_frame: ''
    }
  }
})

// 修正数据初始化结构
const record = ref({
  patient_id: '',
  name: '',
    medical_records: {  // 添加默认结构
     diagnosis: '',
      main_symptoms: '',
      treatment_plan: ''
    },
    prescriptions: {  // 添加默认结构
      drug_name: '',
      specification: 0,
      usage: '',
      status: ''
    },
    uric_acid_levels: {  // 添加默认结构
      current: '',
      trend: '',
      is_miss_drug: '',
      is_change_drug: '',
      is_recurrency: '',
      time_frame: ''
    }
  })


//实验室检查结果图表
const laboratoryRef = ref(null)

let laboratoryInstance = null
// 初始化图表
const initLaboratoryChart = () => {
  if (!laboratoryRef.value) return;

  laboratoryInstance = echarts.init(laboratoryRef.value);

  const option = {
    title: {
      text: '',
      left: 'center',
      textStyle: {
        color: '#333',
        fontSize: 16,
        fontWeight: 'normal'
      }
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'line',
        lineStyle: {
          color: '#7581BD',
          width: 1
        }
      },
      formatter: '{b}<br/>{a}: {c}'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data:patient.value.blood_uric_acid_trend.data.map(item => item.time),
      axisLine: {
        lineStyle: {
          color: '#999'
        }
      },
      axisLabel: {
        color: '#666'
      },
      splitLine: {
        show: false
      }
    },
    yAxis: {
      type: 'value',
      axisLine: {
        show: true,
        lineStyle: {
          color: '#999'
        }
      },
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#eee'
        }
      },
      axisLabel: {
        formatter: '{value}'
      }
    },
    series: [
      {
        name: '数据值',
        type: 'line',
        smooth: true,
        symbol: 'circle',
        symbolSize: 8,
        itemStyle: {
          color: '#0770FF',
          borderColor: '#fff',
          borderWidth: 2
        },
        lineStyle: {
          width: 3,
          color: '#0770FF'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: 'rgba(7, 112, 255, 0.3)'
            },
            {
              offset: 1,
              color: 'rgba(7, 112, 255, 0.1)'
            }
          ])
        },
        data: patient.value.blood_uric_acid_trend.data.map(item => item.value),
        markPoint: {
          data: [
            {type: 'max', name: '最大值'},
            {type: 'min', name: '最小值'}
          ]
        },
        markLine: {
          data: [{type: 'average', name: '平均值'}]
        }
      }
    ]
  };

  laboratoryInstance.setOption(option);
};

// 响应式调整图表大小
const resizeChart = () => {
  laboratoryInstance.resize()
}

onMounted(() => {
  if(route.query.patientId) {
    try {
      // 获取基础信息
      patient.value = getPatientById(route.query.patientId)

      // 获取急诊记录（假设取第一条）
      const records = getEmergencyRecords(route.query.patientId)
      if(records.length > 0) {
        emergencyRecord.value = records[0]
      }

      initLaboratoryChart()
      window.addEventListener('resize', resizeChart)

      console.log('患者基础信息:', patient.value)
      console.log('急诊记录:', emergencyRecord.value)
    } catch (error) {
      console.error('数据加载失败:', error)
    }
  }
})

// 状态样式映射
const statusClass = (status) => {
  const map = {
    '进行中': 'status-success',
    '已完成': 'status-warning',
    '漏服': 'status-danger'
  }
  return map[status] || ''
}

</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
}

body {
  background-color: #f5f7fa;
  color: #333;
  font-size: 14px;
  line-height: 1.5;
}

/* 新增样式 */
.page-title {
  font-size: 20px;
  margin-bottom: 20px;
  color: #333;
}

.indicator-card-purple {
  background-color: #f9f0ff;
  border: 1px solid #d3adf7;
}

/* 调整指标卡片间距 */
.indicators {
  gap: 20px;
}
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 15px;
}

.section-title {
  font-size: 16px;
  font-weight: bold;
  padding: 10px 15px;
  background-color: #f8f9fa;
  border-bottom: 1px solid #e3e8ee;
  margin-bottom: 0;
}

.card {
  background: #fff;
  border-radius: 5px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  overflow: hidden;
}

.card-body {
  padding: 15px;
  .chart {
    height: 200px;
    margin: 15px 0;
    background-color: #f9f9f9;
    border: 1px solid #eaeaea;
    border-radius: 4px;
  }
}

/* 医嘱详情样式 */
.advice-section {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.advice-row {
  display: flex;
}

.advice-label {
  flex: 0 0 100px;
  color: #666;
  font-weight: 500;
}

.advice-content {
  flex: 1;
}

.advice-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
}

.advice-item-number {
  flex: 0 0 20px;
  color: #666;
}

.advice-item-content {
  flex: 1;
}

.advice-text {
  color: #333;
  line-height: 1.8;
}

.medication-record {
  border-top: 1px solid #eee;
  padding-top: 15px;
  margin-top: 5px;
}

.medication-record-item {
  display: flex;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px dashed #eee;
}

.medication-record-item:last-child {
  border-bottom: none;
}

/* 表格样式 */
.table {
  width: 100%;
  border-collapse: collapse;
}

.table th,
.table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.table th {
  font-weight: 500;
  color: #666;
  background-color: #f9f9f9;
}

.table tr:last-child td {
  border-bottom: none;
}

/* 血压图表 */
.chart-container {
  position: relative;
  height: 200px;
  margin: 20px 0;
}

.chart-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.chart-bg-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background-color: #e6e9f0;
}

.chart-y-axis {
  position: absolute;
  top: 0;
  left: 0;
  width: 40px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 5px 0;
  color: #999;
  font-size: 12px;
}

.chart-y-label {
  text-align: right;
  padding-right: 10px;
}

.chart-x-axis {
  position: absolute;
  bottom: -25px;
  left: 40px;
  right: 0;
  display: flex;
  justify-content: space-between;
  color: #999;
  font-size: 12px;
}

.chart-line {
  position: absolute;
  top: 0;
  left: 40px;
  right: 0;
  height: 100%;
  z-index: 2;
}

.chart-data-line {
  stroke: #4a9df8;
  stroke-width: 2;
  fill: none;
}

.chart-data-area {
  fill: rgba(74, 157, 248, 0.1);
}

/* 指标卡片 */
.indicators {
  display: flex;
  gap: 15px;
  margin: 15px 0;
}

.indicator-card {
  flex: 1;
  padding: 15px;
  border-radius: 5px;
  text-align: center;
}

.indicator-card-blue {
  background-color: #f0f7ff;
}

.indicator-card-green {
  background-color: #f0fff4;
}

.indicator-card-orange {
  background-color: #fff7e6;
}

.indicator-value {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 5px;
}

.indicator-title {
  color: #666;
  font-size: 13px;
}

.indicator-change {
  font-size: 12px;
  margin-top: 5px;
}

.text-success {
  color: #52c41a;
}

.text-danger {
  color: #f5222d;
}

.text-warning {
  color: #faad14;
}

.text-primary {
  color: #1890ff;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 15px;
}

.btn {
  padding: 6px 15px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  border: none;
  transition: all 0.3s;
}

.btn-default {
  background-color: #f0f2f5;
  color: #333;
}

.btn-primary {
  background-color: #1890ff;
  color: white;
}

/* 状态标签 */
.status-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 12px;
}

.status-success {
  background-color: #f6ffed;
  border: 1px solid #b7eb8f;
  color: #52c41a;
}

.status-warning {
  background-color: #fffbe6;
  border: 1px solid #ffe58f;
  color: #faad14;
}

.status-danger {
  background-color: #fff2f0;
  border: 1px solid #ffccc7;
  color: #ff4d4f;
}

/* 日期记录 */
.record-date {
  color: #999;
  font-size: 12px;
}

.highlight-link {
  color: #1890ff;
  text-decoration: none;
}

.highlight-link:hover {
  text-decoration: underline;
}
</style>
